/**
 * 
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IPhotoDao;
import com.qiyou.entity.Photo;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * @author 赛艳志
 * 
 */
public class PhotoDaoImpl implements IPhotoDao {
    private Connection conn;

    public PhotoDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(Photo photo) throws SQLException {
        String sql = "insert into tb_photos(paID,photoName,photoDate,photoPath,photoNums) values(?,?,?,?,?)";
        Object[] objects = TransformUtil.beanTOArray(photo, true);
        return DBUtil.execute(conn, sql, objects);
    }

    @Override
    public boolean doUpdate(Photo photo) throws SQLException {
        String sql = "update tb_photos set photoID=?,paID=?,photoName=?,photoDate=?,photoPath=?,photoNums=? "
                + "where photoID=?";
        Object[] objects = TransformUtil.beanTOArray(photo, false);
        return DBUtil.execute(conn, sql, objects, objects[0]);
    }

    @Override
    public boolean doDelete(long photoID) throws SQLException {
        String sql = "delete from tb_photos where photoID=?";
        return DBUtil.execute(conn, sql, photoID);
    }

    @Override
    public Photo getPhoto(long photoID) throws SQLException {
        Photo photo = null;
        String sql = "select * from tb_photos where photoID=?";
        List<Object[]> all = DBUtil.query(conn, sql, photoID);
        if (null != all && all.size() > 0) {
            photo = (Photo) TransformUtil.arrayToBean(all.get(0), Photo.class);
        }
        return photo;
    }

    @Override
    public List<Photo> getPhotos(long albumID) throws SQLException {
        List<Photo> plist = null;
        String sql = "select * from tb_photos where paID=?";
        List<Object[]> all = DBUtil.query(conn, sql, albumID);
        if (null != all && all.size() > 0) {
            plist = new ArrayList<Photo>(all.size());
            Photo photo = null;
            for (Object[] objects : all) {
                photo = (Photo) TransformUtil.arrayToBean(objects, Photo.class);
                plist.add(photo);
            }
        }
        return plist;
    }

    @Override
    public List<Photo> getPhotos(Object... params) throws SQLException {
        List<Photo> plist = null;
        StringBuffer sql = new StringBuffer("select * from tb_photos ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object[]> all = DBUtil.query(conn, sql.toString());
        if (null != all && all.size() > 0) {
            plist = new ArrayList<Photo>(all.size());
            Photo photo = null;
            for (Object[] objects : all) {
                photo = (Photo) TransformUtil.arrayToBean(objects, Photo.class);
                plist.add(photo);
            }
        }
        return plist;
    }

}
